package com.github.rpc4j.consumer.clusters;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class ServiceClustersContext {
	
	private ServiceClustersCall   clustersCall;
	
	private ConcurrentHashMap<String, Set<String>> serviceInfoMap = new ConcurrentHashMap<String, Set<String>>();
	
	
	public ServiceClustersContext (ServiceClustersCall clustersCall){
		this.clustersCall = clustersCall;
	}
    
    public void registerServerList(String serviceKey,Set<String> serverList){
    	
    	serviceInfoMap.put(serviceKey, serverList);
    	
    }
    
    public void deleteServerList(String serviceKey){
    	serviceInfoMap.remove(serviceKey);
    }
    
    
    public void addServer(String serviceKey,String server){
    	Set<String> serverList = serviceInfoMap.get(serviceKey);
    	if(serverList==null){
    		serverList = new HashSet<String>();
    		serviceInfoMap.put(serviceKey, serverList);
    	}
    	serverList.add(server);
    }
     
    
    public void removeServer(String serviceKey,String server){
    	Set<String> serverList = serviceInfoMap.get(serviceKey);
    	if(serverList!=null){
    		serverList.remove(server);
    	}
    }
    
    public String getCallServer(String serviceKey){
    	Set<String> servers = serviceInfoMap.get(serviceKey);
    	return clustersCall.getCallServer(servers);
    }
	

}
